package org.cwnuit.train.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.cwnuit.train.domain.Notices;
import org.cwnuit.train.domain.Trains;
import org.cwnuit.train.domain.Users;
import org.cwnuit.train.utils.ConnDB;
import org.cwnuit.train.utils.EntityUtil;
import org.cwnuit.train.utils.PageModel;

@SuppressWarnings("all")
public class NoticesDAO {
	
	private static Logger logger = Logger.getLogger(NoticesDAO.class.getName());   
	//添加通知信息
	public boolean addNotice(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.executeUpdate(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}
	
	//删除通知信息
	public boolean deleteNotice(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.execute(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}
	
	//修改通知信息
	public boolean updateNotice(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.executeUpdate(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}
	
	// 查找通知信息的方法
	public Notices findNotice(String SQL) {

		Notices notice = null;
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		ResultSet rs = ConnDB.getResultSet(stmt, SQL);

		try {
			while (rs.next()) {
				notice = EntityUtil.RsToNotice(rs);
			}
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return notice;
	}
	
	 //查找通知信息的方法
	public List<Notices> findNotices(String SQL) {

		List<Notices> userslist = new ArrayList();
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		ResultSet rs = ConnDB.getResultSet(stmt, SQL);
		
		try {
			while(rs.next())
			{
				userslist.add(EntityUtil.RsToNotice(rs));
			}
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return userslist;
	}
	
	//分页查找通知信息
	public PageModel findByPage(int offset, int pagesize,String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		String tempSQL = SQL + " LIMIT " + offset + " , " + pagesize;
		ResultSet rs = ConnDB.getResultSet(stmt, tempSQL);
		
		PageModel pm = new PageModel();
		List<Notices> noticeslist = new  ArrayList();
		try {
			while(rs.next())
			{
				noticeslist.add(EntityUtil.RsToNotice(rs));	 
			}
			pm.setTotal(countNum(SQL));
			pm.setDatas(noticeslist);
			logger.debug(tempSQL);
		} catch (SQLException e) {
			e.printStackTrace();
			
		}finally
		{
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return pm;
	}
	
	// 查找数据库中的记录数
	public int countNum(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		ResultSet rs = ConnDB.getResultSet(stmt, SQL);
		int counter = 0;
		try {
			while (rs.next()) {
				counter += 1;
			}
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return counter;
	}
}
